<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

function e107shop_admin_list_coupons() {
    global $e107shop_db_name, $sql, $ns;

        if(!$sql->db_Select($e107shop_db_name['coupons'], "*", "")) {
            $ns->tablerender("<center>".lan_admin_menu_coupons."</center>", 
            "<br /><center>".lan_admin_coupons_error."</center><br />");
            return;
        } else {    
            
            $display="<center><form name='form1'><br />
                        <table class='fborder' width='95%'>
                            <tr>
                                <td class='forumheader'>
                                    ".lan_manage_coupons_title."
                                </td>
                            </tr>
                            <tr>
                                <td class='forumheader2'>
                                    ".lan_coupon_name.": <select class=\"tbox\" name='p' id='p'>";
                                
            while($row=$sql->db_Fetch()){
                    $display.="<option value='".$row["coupon_code"]."'>"
                    .$row["coupon_name"]."</option>";
            }
                
            
            $display.="</option>
            </select>";

            $display.="<input class=\"tbox\" type='button' name='a' value='"
            .lan_global_edit."' OnClick=\"window.location='admin_manage_coupons.php?a=1&p='
            +document.form1.p.options[document.form1.p.selectedIndex].value\">";
            $display.="<input class=\"tbox\" type='button' name='d' value='"
            .lan_global_delete."' OnClick=\"window.location='admin_manage_coupons.php?a=2&p='
            +document.form1.p.options[document.form1.p.selectedIndex].value\">";
            $display.="</form>";
            $display.="</td></tr></table>";
        }
    $ns->tablerender("<center>".lan_coupon_manage, $display);
}


function e107shop_manage_coupons($action, $coupon) {
    global $e107shop_db_name, $sql, $ns;
    if($action==2) {
        $sql->db_Delete($e107shop_db_name['coupons'], "coupon_code='".$coupon."'");
        
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center><br />".lan_global_deleted."</center>");
        $formaction = 2;
    } else if($action==1) {
        //
        //edit the product
        //
        $sql->db_Select($e107shop_db_name['coupons'], "*", "coupon_code='".$coupon."'");
        while($row=$sql->db_Fetch()) {
            //
            // Store the fields in variables so they can be
            // used in the form, without having to write out the
            // form twice.
            //
            $coupon_name = $row["coupon_name"];
            $coupon_code = $row["coupon_code"];
            $coupon_expiry_date = $row["coupon_expiry_date"];
            $coupon_used = $row["coupon_used_x_times"];
            $valid_for = $row["xvalid_for_uses"];    
            $coupon_discount_amount = $row["coupon_discount_amount"];
            $coupon_discount_type = $row["coupon_discount_type"];
            $coupon_expiry = date("m/d/Y", $coupon_expiry_date);
        }
        $formaction = 1;
    } else {
        $formaction = 2;
    }

    //
    // Display form
    //

    $display = "<center><form action='".e_SELF."' method='POST' name='productform' ID='productform'>
                <br />
                <table width='95%' class='fborder'>
                    <tr>    
                        <td class='forumheader2' width='20%'>
                            ".lan_coupon_name.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='coupon_name' value='"
                            .$coupon_name."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_coupon_code.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='coupon_code' value='"
                            .$coupon_code."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_coupon_exp.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='coupon_expiry' value='"
                            .$coupon_expiry."'>
                            &nbsp;".lan_admin_coupon_exp_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_coupon_type.": 
                        </td>
                        <td class='forumheader3'>
                            <select class=\"tbox\" type='text' name='coupon_type'>";
                            if($coupon_discount_type=="1"){
                                $display.="<option selected value='1'>-</option>
                                        <option value='2'>%</option>";
                            }
                            else
                            {
                                $display.="<option selected value='2'>%</option>
                                        <option value='1'>-</option>";
                            }
                        $display.="</select>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_coupon_discount.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='discount' value='".$coupon_discount_amount."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_coupon_used.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='coupon_used' value='".$coupon_used."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_coupon_valid.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='coupon_valid' value='"
                            .$valid_for."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader' colspan='2'>
                            <input class=\"tbox\" type='hidden' value='"
                            .$formaction."' name='faction'>
                            <input class=\"tbox\" type='hidden' value='"
                            .$coupon."' name='coupo'><center>";
    
    if ($action == 1) {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_update."' name='subbed'>";
    } else {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_submit."' name='subbed'>";
    }
    
    $display .= "</center></td></tr></form></table>";
    
    if ($action == 1) {
        $ns->tablerender("<center>".lan_manage_coupons_edit."</center>", $display);
    } else {
        $ns->tablerender("<center>".lan_manage_coupons_add."</center>", $display);
    }
}


function e107shop_process_coupon($action, $coupo, $coupon_name, $coupon_code, $expiry, $type, $used, 
$use_allowed, $amount) {
    global $e107shop_db_name;

    $expiry = explode("/", $expiry);
    $expiry=mktime(0,0,0,$expiry[0], $expiry[1], $expiry[2]);
    if ($used == '') $used = 0;
    
    global $sql, $ns;
    if($action==1) {
    
        //
        // edit
        //
        $sql->db_Update($e107shop_db_name['coupons'], "coupon_name='".$coupon_name."', coupon_code='"
        .$coupon_code."', coupon_expiry_date=".$expiry.", coupon_used_x_times="
        .$used.", xvalid_for_uses='".$use_allowed."', coupon_discount_amount="
        .$amount.", coupon_discount_type='".$type."' where coupon_code='".$coupo."'");
        
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center>".lan_global_updated."</center>");
    } else {
        //
        // Insert a new product
        //
        $sql->db_Insert($e107shop_db_name['coupons'], "'".$coupon_name."', '".$coupon_code."', "
        .$expiry.", ".$used.", '".$use_allowed."', ".$amount.", '".$type."'");
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center>".lan_admin_coupon_created."</center>");
    }
}
